using Microsoft.Phone.Info;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace Coding4Fun.Phone.Controls
{
	public class MemoryCounter : Control
	{
		private const float float_0 = 1048576f;
		private readonly DispatcherTimer dispatcherTimer_0;
		private bool bool_0;
		public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register("UpdateInterval", typeof(int), typeof(MemoryCounter), new PropertyMetadata(1000, new PropertyChangedCallback(MemoryCounter.OnUpdateIntervalChanged)));
		public static readonly DependencyProperty CurrentMemoryProperty = DependencyProperty.Register("CurrentMemory", typeof(string), typeof(MemoryCounter), new PropertyMetadata("0"));
		public static readonly DependencyProperty PeakMemoryProperty = DependencyProperty.Register("PeakMemory", typeof(string), typeof(MemoryCounter), new PropertyMetadata("0"));
		public int UpdateInterval
		{
			get
			{
				return (int)base.GetValue(MemoryCounter.UpdateIntervalProperty);
			}
			set
			{
				base.SetValue(MemoryCounter.UpdateIntervalProperty, value);
			}
		}
		public string CurrentMemory
		{
			get
			{
				return (string)base.GetValue(MemoryCounter.CurrentMemoryProperty);
			}
			set
			{
				base.SetValue(MemoryCounter.CurrentMemoryProperty, value);
			}
		}
		public string PeakMemory
		{
			get
			{
				return (string)base.GetValue(MemoryCounter.PeakMemoryProperty);
			}
			set
			{
				base.SetValue(MemoryCounter.PeakMemoryProperty, value);
			}
		}
		public MemoryCounter()
		{
			if (Debugger.get_IsAttached())
			{
				base.set_DefaultStyleKey(typeof(MemoryCounter));
				base.set_DataContext(this);
				DispatcherTimer dispatcherTimer = new DispatcherTimer();
				dispatcherTimer.set_Interval(TimeSpan.FromMilliseconds((double)this.UpdateInterval));
				this.dispatcherTimer_0 = dispatcherTimer;
				this.dispatcherTimer_0.add_Tick(new EventHandler(this.dispatcherTimer_0_Tick));
				this.dispatcherTimer_0.Start();
				return;
			}
			base.set_Visibility(1);
		}
		private static void OnUpdateIntervalChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (Debugger.get_IsAttached())
			{
				MemoryCounter memoryCounter = (MemoryCounter)dependencyObject_0;
				if (memoryCounter != null && memoryCounter.dispatcherTimer_0 != null)
				{
					memoryCounter.dispatcherTimer_0.set_Interval(TimeSpan.FromMilliseconds((double)((int)dependencyPropertyChangedEventArgs_0.get_NewValue())));
				}
			}
		}
		private void dispatcherTimer_0_Tick(object sender, EventArgs e)
		{
			if (!Debugger.get_IsAttached())
			{
				if (!this.bool_0)
				{
					return;
				}
			}
			try
			{
				this.CurrentMemory = ((float)DeviceStatus.get_ApplicationCurrentMemoryUsage() / 1048576f).ToString("#.00");
				this.PeakMemory = ((float)DeviceStatus.get_ApplicationPeakMemoryUsage() / 1048576f).ToString("#.00");
			}
			catch (Exception)
			{
				this.bool_0 = true;
				this.dispatcherTimer_0.Stop();
			}
		}
	}
}
